# coding=utf-8

from datetime import datetime
from flask import current_app, flash, redirect, render_template, session
from app.get_task_datas import get_datas, getLastYearMon, getMonthFirstDay
from . import main
from config import config


# 检查session
def checkSession():
    if 'username' in session:
        return 0
    else:
        return 1


# HOME
@main.route('/')
def index():
    if checkSession() == 0:
        return render_template('index.html', username=session['username'], current_time=datetime.utcnow())
    else:
        flash('you are not logged in')
        return render_template('login.html')


# 菜单页
@main.route('/task/<name>')
def task(name):
    if checkSession() == 0:

        lastYearMonStr = getLastYearMon()
        firstDay = getMonthFirstDay()
        firstDayStr = firstDay.strftime('%Y%m%d')

        # 携转工单
        if name == "NetnpTrans":
            praSql = config['default'].NETNPTRANS_SQL
            current_app.logger.warn('praSql: %s', praSql)

            datas = get_datas(name, praSql)
            current_app.logger.info(datas)
            return render_template('task/' + name + '.html', name=name, username=session['username'],
                                   count_7070=datas[0],
                                   count_7080=datas[1],
                                   succ_count_7070=datas[2],
                                   succ_count_7080=datas[3],
                                   err_count=datas[4],
                                   current_time=datetime.utcnow())

        # 积分月末镜像备份
        elif name == "ScoreBackup":
            current_app.logger.info('This is ScoreBackup')
            return render_template('task/' + name + '.html', name=name, username=session['username'], bill_month=lastYearMonStr, current_time=datetime.utcnow())

        # 批量销账
        elif name == "WriteOff_DataCheck":
            current_app.logger.info('This is WriteOff_DataCheck')
            return render_template('task/' + name + '.html', name=name, username=session['username'], yyyymmdd=firstDayStr, current_time=datetime.utcnow())

        elif name == "WriteOff_GetSFiles":
            current_app.logger.info('This is WriteOff_GetSFiles')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

        elif name == "WriteOff_StartCalc":
            current_app.logger.info('This is WriteOff_StartCalc')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

        elif name == "WriteOff_BakupFiles":
            current_app.logger.info('This is WriteOff_BakupFiles')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

        elif name == "WriteOff_Files2Db":
            current_app.logger.info('This is WriteOff_Files2Db')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

        # 批销平衡性检查
        elif name == "WriteOff_BalanceCheck":
            current_app.logger.info('This is WriteOff_BalanceCheck')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

        # 积分计算
        elif name == "ScoreCalc_DataCheck":
            current_app.logger.info('This is ScoreCalc_DataCheck')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

        elif name == "ScoreCalc_Filter":
            current_app.logger.info('This is ScoreCalc_Filter')
            return render_template('task/' + name + '.html', name=name, username=session['username'], bill_month=lastYearMonStr, current_time=datetime.utcnow())

        elif name == "ScoreCalc_StartCalc":
            current_app.logger.info('This is ScoreCalc_StartCalc')
            return render_template('task/' + name + '.html', name=name, username=session['username'], bill_month=lastYearMonStr, current_time=datetime.utcnow())

        elif name == "ScoreCalc_File2DB":
            current_app.logger.info('This is ScoreCalc_File2DB')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

        elif name == "ScoreCalc_Effect":
            current_app.logger.info('This is ScoreCalc_Effect')
            return render_template('task/' + name + '.html', name=name, username=session['username'], bill_month=lastYearMonStr, current_time=datetime.utcnow())

        else:
            current_app.logger.info('This is else')
            return render_template('task/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())

    else:
        flash('you are not logged in')
        return render_template('login.html')


# 备份页
@main.route('/bakup/<name>')
def bakup(name):
    if checkSession() == 0:
        return render_template('bakup/' + name + '.html', name=name, username=session['username'], current_time=datetime.utcnow())
    else:
        flash('you are not logged in')
        return render_template('login.html')


# 调度平台
@main.route('/aischeduler')
def aischeduler():
    if checkSession() == 0:
        ai_scheduler = config['default'].AISCHEDULER_URL
        return redirect(ai_scheduler)
    else:
        flash('you are not logged in')
        return render_template('login.html')


# 错误重定向
@main.app_errorhandler(404)
def page_not_found(e):
    current_app.logger.error('404 %s', e)
    return render_template('error/404.html', username=session['username']), 404


@main.app_errorhandler(500)
def internal_server_error(e):
    current_app.logger.error('500 %s', e)
    return render_template('error/500.html', username=session['username']), 500
